package com.ucsd.cse135.gas;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

import com.ucsd.cse135.db.Database;

public class Degree {

	private int degreeId;
	private int applicantId;
	private int locationId;
	private String location; 
	private int universityId;
	private String university;
	private int disciplineId;
	private String discipline;
	private int month;
	private int year;
	private float gpa;
	private String title;
	private int titleId;
	private String transcription;
	
	public int getDegreeId() {
		return degreeId;
	}
	public void setDegreeId(int degreeId) {
		this.degreeId = degreeId;
	}
	public int getApplicantId() {
		return applicantId;
	}
	public void setApplicantId(int applicantId) {
		this.applicantId = applicantId;
	}
	public int getLocationId() {
		return locationId;
	}
	public void setLocationId(int locationId) {
		this.locationId = locationId;
	}
	public String getLocation() {
		return location;
	}
	public void setLocation(String location) {
		this.location = location;
	}
	public int getUniversityId() {
		return universityId;
	}
	public void setUniversityId(int universityId) {
		this.universityId = universityId;
	}
	public String getUniversity() {
		return university;
	}
	public void setUniversity(String university) {
		this.university = university;
	}
	public int getDisciplineId() {
		return disciplineId;
	}
	public void setDisciplineId(int disciplineId) {
		this.disciplineId = disciplineId;
	}
	public String getDiscipline() {
		return discipline;
	}
	
	public void setDiscipline(String discipline) {
		this.discipline = discipline;
		
		try {
			Connection conn = Database.open();
			
			String sql = "SELECT id FROM discipline WHERE name = ?;";
			
			PreparedStatement preStmt = conn.prepareStatement(sql);
			preStmt.setString(1, discipline);
			
			ResultSet rs = preStmt.executeQuery();
			
			while(rs.next())
			{
				this.disciplineId = rs.getInt("id");
			}
			
			rs.close();
			preStmt.close();
			Database.close();
			
		}
		catch(Exception e)
		{
			Database.close();
		}
	}
	public int getMonth() {
		return month;
	}
	public void setMonth(int month) {
		this.month = month;
	}
	public int getYear() {
		return year;
	}
	public void setYear(int year) {
		this.year = year;
	}
	public float getGpa() {
		return gpa;
	}
	public void setGpa(float gpa) {
		this.gpa = gpa;
	}
	public String getTitle() {
		return title;
	}
	
	public void setTitle(String title) {
		this.title = title;
		
		try {
			Connection conn = Database.open();
			
			String sql = "SELECT id FROM title WHERE name = ?;";
			
			PreparedStatement preStmt = conn.prepareStatement(sql);
			preStmt.setString(1, title);
			
			ResultSet rs = preStmt.executeQuery();
			
			while(rs.next())
			{
				this.titleId = rs.getInt("id");
			}
			
			rs.close();
			preStmt.close();
			Database.close();
		}
		catch(Exception e)
		{
			Database.close();
		}
	}
	public int getTitleId() {
		return titleId;
	}
	public void setTitleId(int titleId) {
		this.titleId = titleId;
	}
	public String getTranscription() {
		return transcription;
	}
	public void setTranscription(String transcription) {
		this.transcription = transcription;
	}

	/** Method to write all information to database **/
	
	public void writeToDatabase() {
		
		try {
			
			Connection conn = Database.open();
			
			String sql = "INSERT INTO degree (month, year, gpa, applicant, title, discipline, university) " +
						 "VALUES (?, ?, ?, ?, ?, ?, ?);";
			
			PreparedStatement preStmt = conn.prepareStatement(sql);
			preStmt.setInt(1, this.getMonth());
			preStmt.setInt(2, this.getYear());
			preStmt.setFloat(3, this.getGpa());
			preStmt.setInt(4, this.getApplicantId());
			preStmt.setInt(5, this.getTitleId());
			preStmt.setInt(6, this.getDisciplineId());
			preStmt.setInt(7, this.getUniversityId());
			
			preStmt.executeUpdate();
			
			preStmt.close();
			Database.close();
			
		}
		catch(Exception e) {
			
			Database.close();
			e.printStackTrace();
		}
		
	}
}
